package com.hnust.fiveonesix.Controller.ShopInfoController;

import com.hnust.fiveonesix.POJO.Product;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ProductController {

    //准备商品数据
    private static List<Product> list;

    static {
        list = new ArrayList<Product>();
        list.add(new Product(1, "李子七面条", 1));
        list.add(new Product(2, "亲嘴烧", 2));
        list.add(new Product(3, "小蛋糕", 3));
        list.add(new Product(4, "紫薯片", 2));
        list.add(new Product(5, "猪肉脯", 2));
        list.add(new Product(6, "三只松鼠", 4));
        list.add(new Product(7, "果蔬脆片", 3));
        list.add(new Product(8, "果冻", 0.5));
        list.add(new Product(9, "麻花", 2));
        list.add(new Product(10, "可口可乐", 0.5));
        list.add(new Product(11, "乐事薯片", 1));
        list.add(new Product(12, "麻辣小龙虾", 5));
        list.add(new Product(13, "狐狸和兔子", 10));
        list.add(new Product(14, "蓝皮怪", 10));
        list.add(new Product(15, "建筑物积木", 9));
        list.add(new Product(16, "梵高向日葵系列油画", 319.5));
        list.add(new Product(17, "可爱兔子玩偶", 8));
        list.add(new Product(18, "小玩偶（单个）", 1));
        list.add(new Product(19, "中型家园主题油画", 20));
        list.add(new Product(20, "城堡积木", 11));
        list.add(new Product(21, "小人装饰", 3));
        list.add(new Product(22, "稻草人玩偶", 7));
        list.add(new Product(23, "生日贺卡（可展开）", 3));
        list.add(new Product(24, "可爱少女大玩偶", 13));
        list.add(new Product(25, "Nike橙色跑鞋", 120));
        list.add(new Product(26, "Nike Air Max 2090", 135));
        list.add(new Product(27, "PG 4 PCG EP", 120));
        list.add(new Product(28, "Kyrie 6 EP", 134));
        list.add(new Product(29, "Nike AlphaDunk EP", 200));
        list.add(new Product(30, "Air Jordan XXXIV Low EP PF", 210));
        list.add(new Product(31, "ADIZERO BOSTON 9 W 跑步运动鞋", 130));
        list.add(new Product(32, "NITE JOGGER 经典运动鞋", 190));
        list.add(new Product(33, "ADIDAS NEO BRAVADA 休闲运动鞋", 70));
        list.add(new Product(34, "YUNG-1 经典运动鞋", 137));
        list.add(new Product(35, "3MC X DISNEY SPORT GOOFY DISNEY高飞联名系列经典运动鞋", 100));
        list.add(new Product(36, "SUPERSTAR W 经典运动鞋", 140));
    }


    //清空购物车
    @RequestMapping("/delCart")
    public String  delCart(HttpServletRequest request){
        request.getSession().removeAttribute("cartMap");
        Map<Product, Integer> map =new HashMap<>();
        allmoney=0;
        request.getSession().setAttribute("cartMap",map);
        return "cart";
    }

    private static double allmoney=0;
    //增加商品到购物车
    @RequestMapping("/addToCart")
    public String addToCart(HttpServletRequest request, int index) {


        HttpSession session = request.getSession();

        //1. 根据索引，获取到要添加到购物车的商品
        Product product = list.get(index-1);
        if(index<=12){
            product.setImg("foods/"+index+".jpg");
        }
        else if(index<=24){
            product.setImg("others/"+(index-12)+".jpg");
        }
        else {
            product.setImg("shoes/"+(index-24)+".jpg");
        }

        //2. 加入购物车 购物车其实就是商品和数量的对应关系。
        Map<Product, Integer> map = (Map<Product, Integer>) session.getAttribute("cartMap");
        //第一次来，连购物车都没有
        if (map == null) {
            map = new HashMap<Product, Integer>();
        }

        //判断购物车里面是否有该商品 ，如果有  数量 + 1  ，如果没有，数量-1
        if (map.containsKey(product)) {
            map.put(product, map.get(product) + 1);
        } else {
            map.put(product, 1);
        }

        allmoney+=product.getPrice();
        //3. 存储到session中。
        request.getSession().setAttribute("cartMap", map);
        request.getSession().setAttribute("xiaoji",allmoney);
        request.getSession().setAttribute("allmone",allmoney);


    return "cart";
    }

    @RequestMapping("/ok")
    public String chosesend(HttpServletRequest request){
        Map<Product, Integer> map = (Map<Product, Integer>) request.getSession().getAttribute("cartMap");
        if(map==null)
            return "forward:/空购物车/nullbuy.html";
        Set<Product> products = map.keySet();
        if(products.isEmpty())
            return "forward:/空购物车/nullbuy.html";
         else
            return "cart2";
    }

    @RequestMapping("/sendway")
    public String finamoney(HttpServletRequest request,@RequestParam("sc") String src){
        Integer sendmoney;
        HttpSession session = request.getSession();
        if(src.equals("隔日到达")) {
            sendmoney = 4;
            session.setAttribute("senmon", "$4");
        }
        else if(src.equals("标准配送")) {
            sendmoney = 2;
            session.setAttribute("senmon", "$2");
        }
        else {
            sendmoney = 0;
            session.setAttribute("senmon", "$0");
        }
        Map<Product, Integer> map = (Map<Product, Integer>) session.getAttribute("cartMap");
            session.setAttribute("allmone",allmoney+sendmoney);
            return "cart2";
    }
}
